﻿package
{
	import flash.display.MovieClip;
	import fl.transitions.easing.Bounce;
	import fl.transitions.easing.Elastic;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import flash.events.MouseEvent;
	
	public class BtnRoll_BounceReflective
	{
		//VARIABLES INTERNAS SETEABLES.
		private var clip:MovieClip;
		private var useHand_bool:Boolean = true;
		private var bounceHeight_num:Number = 15;
		private var pickUpTime_num:Number = 1;
		private var dropTime_num:Number = 0.5;
		private var gradientRatio_uint:uint = 150;
		private var reflectionAlpha_num:Number = 0.25;
		private var offset_num:Number = 0;
		
		//VARIABLES INTERNAS.
		private var reflection_mc:MovieClip;
		private var initY:Number;
		private var yTween:Tween;
		
		//SETTERS Y GETTERS.
		public function set targetClip(value:MovieClip):void {clip = value;}
		public function set useHand(value:Boolean):void {useHand_bool = value;}
		public function set bounceHeight(value:Number):void {bounceHeight_num = value;}
		public function set pickUpTime(value:Number):void {pickUpTime_num = value;}
		public function set dropTime(value:Number):void {dropTime_num = value;}
		public function set gradientRatio(value:uint):void {gradientRatio_uint = value;}
		public function set reflectionAlpha(value:Number):void {reflectionAlpha_num = value;}
		public function set reflectionOffset(value:Number):void {offset_num = value;}
		
		//CONSTRUCTOR.
		public function BtnRoll_BounceReflective() {}
		
		//INIT.
		public function init()
		{
			//SETEA LA MANITO.
			clip.hit.buttonMode = useHand_bool;
			clip.hit.useHandCursor = useHand_bool;
			
			//REGISTRA LA POSICION INICIAL.
			initY = clip.subClip.y;
			
			//CONSTRUYE EL REFLEJO.
			var reflection:Reflection = new Reflection();
			reflection.targetClip = clip;
			reflection.gradientRatio = gradientRatio_uint;
			reflection.reflectionAlpha = reflectionAlpha_num;
			reflection.reflectionOffset = offset_num;
			reflection_mc = reflection.init();
			
			//ACTIVA EL ROLL OVER Y ROLL OUT DEL CLIP.
			clip.hit.addEventListener(MouseEvent.MOUSE_OVER, rollOverHandler);
			clip.hit.addEventListener(MouseEvent.MOUSE_OUT, rollOutHandler);
		}
		
		//MOUSE HANDLERS.
		private function rollOverHandler(event:MouseEvent)
		{
			yTween = new Tween(clip.subClip, "y", Elastic.easeOut, clip.subClip.y, initY - bounceHeight_num, pickUpTime_num, true);
			yTween.addEventListener(TweenEvent.MOTION_CHANGE, reflectionMotion);
		}
		private function rollOutHandler(event:MouseEvent)
		{
			yTween.stop();
			var easeType:Function = offset_num > 0 ? Elastic.easeOut : Bounce.easeOut;
			var time:Number = offset_num > 0 ? pickUpTime_num : dropTime_num;
			yTween = new Tween(clip.subClip, "y", easeType, clip.subClip.y, initY, time, true);
			yTween.addEventListener(TweenEvent.MOTION_CHANGE, reflectionMotion);
		}
		
		//CONTROLA EL MOVIMIENTO DEL REFLEJO.
		private function reflectionMotion(event:TweenEvent)
		{
			reflection_mc.y = clip.subClip.height/2 - clip.subClip.y;
		}
	}
}